﻿using InfonavaEye.common;
using InfonavaEye.model.wjapi;
using InfonavaEye.model;
using InfonavaEye.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using static InfonavaEye.handler.AutoCaptrueHandler;

namespace InfonavaEye.components
{
    public partial class OrderListPanel : UserControl
    {


        private String orderType= "PUR_ORDER_HEADER";
        private Boolean isQureyOrder=false;

        public delegate void OrderViewChangeDelegate(String eventCode, String orderNo);
        public event OrderViewChangeDelegate orderViewChangeDelegate;

        public OrderListPanel()
        {
            InitializeComponent();
           
        }

        private void orderListView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                //添加行号 
                SolidBrush v_SolidBrush = new SolidBrush(this.orderListView.RowHeadersDefaultCellStyle.ForeColor);
                int v_LineNo = 0;
                v_LineNo = e.RowIndex + 1;
                string v_Line = v_LineNo.ToString();
                e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);

            }
            catch (Exception ex)
            {
                Console.WriteLine("添加行号时发生错误，错误信息：" + ex.Message, "操作失败");
            }
        }
        private void orderListView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ( e.RowIndex == -1)
            {
                return;
            }
           DataGridViewRow row= this.orderListView.Rows[e.RowIndex];
           orderViewChangeDelegate.Invoke("selectd",row.Cells[0].Value.ToString());
        }
        private void orderListPanelLoad(object sender, EventArgs e) {
            this.getOrderList();
        }

        private void getOrderList() {
            if (isQureyOrder) {
                return;
            }
            this.isQureyOrder = true;
            enableRadio();
            Task.Factory.StartNew(() =>
            {   
                var service = ReportServiceFactory.getService();
                List<OrderListRespDto> orderList = service.pullOrderList(orderType);
                this.isQureyOrder = false;
                enableRadio();
                if (orderList == null)
                {
                    return;
                }
                this.orderListView.AutoGenerateColumns = false;

                this.Invoke(new Action(delegate {
                    // 绑定数据源
                    this.orderListView.DataSource = new BindingSource(new BindingList<OrderListRespDto>(orderList), null);
                    //使用委托更新按钮状态
                    this.orderViewChangeDelegate.Invoke("updateBtnStatu", "");
                    
                }));

            });

        }
        private void enableRadio() {
            if (this.isQureyOrder)
            {
                this.purOrderheaderRadio.Enabled = false;
                this.surGeryOrderRadio.Enabled = false;
            }
            else {
                this.purOrderheaderRadio.Enabled = true;
                this.surGeryOrderRadio.Enabled = true;
            }
        }

        private void purOrderheaderRadio_CheckedChanged(object sender, EventArgs e)
        {
            if (this.purOrderheaderRadio.Checked) {
                this.orderType = "PUR_ORDER_HEADER";
                this.getOrderList();
            }
         
        }

        private void surGeryOrderRadio_CheckedChanged(object sender, EventArgs e)
        {

            if (this.surGeryOrderRadio.Checked)
            {
                this.orderType = "SURGERY_ORDER";
                this.getOrderList();
            }
        }
    }


}
